package com.bbh.plugin.single.privilege.demo.client;

import com.bbh.plugin.single.privilege.core.entity.Account;
import com.bbh.plugin.single.privilege.core.entity.Resource;
import com.google.common.collect.Lists;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.boot.config.shiro.base.entity.ShiroAccount;
import org.springframework.boot.config.shiro.base.entity.ShiroResource;

import java.util.List;

/**
 * Created by roman_000 on 2015/10/3.
 */
public abstract class RbacHelper {

    public static ShiroAccount convert2ShiroAccount(Account obj) {
        ShiroAccount entity = new ShiroAccount();
        entity.setId(obj.getId());
        entity.setLoginName(obj.getLoginName());
        entity.setPassword(obj.getPassword());
        entity.setIsEnabled(obj.getIsEnabled());
        entity.setIsLocked(obj.getIsLocked());
        entity.setLoginFailureCount(obj.getLoginFailureCount());
        entity.setLockedTime(obj.getLockedTime());
        entity.setLoginTime(obj.getLoginTime());
        entity.setLoginIp(obj.getLoginIp());
        return entity;
    }


    public static ShiroResource convert2ShiroResource(Resource obj) {
        ShiroResource entity = new ShiroResource();
        entity.setId(obj.getId());
        entity.setParentId(obj.getParentId());
        entity.setName(obj.getName());
        entity.setSort(obj.getSort());
        entity.setUrl(obj.getUrl());
        entity.setTarget(obj.getTarget());
        entity.setIcon(obj.getIcon());
        entity.setType(obj.getType());
        entity.setPermissionKey(obj.getPermissionKey());
        entity.setPermissionValue(obj.getPermissionValue());
        return entity;
    }


    public static List<ShiroResource> convert2ShiroResourceList(List<Resource> resourceList) {
        List<ShiroResource> shiroResourceList = Lists.newArrayList();
        if (CollectionUtils.isNotEmpty(resourceList)) {
            for (Resource resource : resourceList) {
                shiroResourceList.add(convert2ShiroResource(resource));
            }
        }
        return shiroResourceList;
    }
}
